﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;

namespace WorkRecord.CommonClass
{
    public class PageBase : Page
    {
        protected override void OnError(EventArgs e)
        {
            //Exception ex = Server.GetLastError();
            //Response.Redirect(CommonMethod.GetRedirectUrl(RedirectMessage.Error, RedirectType.None));
            //Server.ClearError();
        }
        protected void ShowAlert(string msg, bool hasUpdatePanel = false, string key = null)
        {
            if (hasUpdatePanel)
            {
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), key ?? Guid.NewGuid().ToString(),
                    "alert('" + msg + "');", true);
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(),
                    string.Format("alert('{0}');", msg), true);
            }
        }

        /// <summary>
        /// 显示消息提示对话框，并进行页面跳转
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        /// <param name="url">跳转的目标URL</param>
        public static void ShowAndRedirect(Page page, string msg, string url)
        {
            var builder = new StringBuilder();
            builder.Append("<script language='javascript' defer>");
            builder.AppendFormat("alert('{0}');", msg);
            builder.AppendFormat("window.location.href='{0}'", url);
            builder.Append("</script>");
            page.ClientScript.RegisterClientScriptBlock(page.GetType(), "message", builder.ToString());

        }
    }
}